import { PlatformHelper, PlatformType } from './modules/platformHelper';
import { EnvironmentDetector } from './modules/environmentDetector';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';

/**
 * 增强平台功能测试 - 跨平台支持（Windows/WSL2和macOS）
 */
async function testCrossPlatformSupport(): Promise<void> {
    console.log('========== 开始测试增强跨平台支持功能 ==========\n');

    // 获取平台帮助器实例和环境检测器
    const platformHelper = PlatformHelper.getInstance();
    const environmentDetector = new EnvironmentDetector();
    
    // 测试平台检测
    console.log('----- 测试平台检测 -----');
    const platformType = platformHelper.getPlatformType();
    console.log(`检测到的平台类型: ${platformType}`);
    console.log(`操作系统: ${os.platform()} ${os.release()}`);
    console.log(`架构: ${os.arch()}`);
    
    if (platformHelper.isWindows()) {
        console.log('✅ 当前是 Windows 平台');
        // Windows 特定测试
        await testWindowsSpecific();
    } else if (platformHelper.isMacOS()) {
        console.log('✅ 当前是 macOS 平台');
        // macOS 特定测试
        await testMacOSSpecific();
    } else if (platformHelper.isWSLEnvironment()) {
        console.log('✅ 当前是 WSL 环境');
        // WSL 特定测试
        await testWSLSpecific();
    } else if (platformHelper.isLinux()) {
        console.log('✅ 当前是 Linux 平台');
    }
    
    // 测试环境检测与平台集成
    console.log('\n----- 测试环境检测与平台集成 -----');
    const env = await environmentDetector.detect();
    console.log('检测到的环境:', env);
    
    if (env.platform) {
        console.log(`平台类型: ${env.platform.type}`);
        console.log(`WSL环境: ${env.platform.isWSL ? '是' : '否'}`);
        console.log(`Rootless: ${env.platform.isRootless ? '是' : '否'}`);
    }
    
    // 测试端口权限
    console.log('\n----- 测试端口权限处理 -----');
    const testPorts = [80, 443, 3000, 8080];
    for (const port of testPorts) {
        const isPrivileged = platformHelper.isPrivilegedPort(port);
        const suggestedPort = platformHelper.suggestNonPrivilegedPort(port);
        console.log(`端口 ${port} - 特权端口: ${isPrivileged ? '是' : '否'}, 建议端口: ${suggestedPort}`);
    }
    
    // 测试容器命令适配
    console.log('\n----- 测试容器命令适配 -----');
    const dockerOptions = platformHelper.getContainerOptions('docker');
    const podmanOptions = platformHelper.getContainerOptions('podman');
    
    // 生成适配后的命令示例
    const dockerOptionsStr = Object.values(dockerOptions).join(' ');
    const podmanOptionsStr = Object.values(podmanOptions).join(' ');
    
    console.log(`Docker 命令示例: docker ${dockerOptionsStr} run -d -p 8080:80 nginx:alpine`);
    console.log(`Podman 命令示例: podman ${podmanOptionsStr} run -d -p 8080:80 nginx:alpine`);
    
    // 测试路径转换
    console.log('\n----- 测试路径转换 -----');
    const testPaths = [
        __dirname,
        path.join(os.homedir(), 'nginx'),
        '/etc/nginx/conf.d/default.conf',
        'C:\\nginx\\conf\\nginx.conf'
    ];
    
    for (const originalPath of testPaths) {
        try {
            const containerPath = await platformHelper.convertPathForContainer(originalPath);
            console.log(`原始路径: ${originalPath}`);
            console.log(`容器路径: ${containerPath}`);
            console.log('---');
        } catch (error) {
            console.error(`转换路径失败: ${originalPath}`, error);
        }
    }
    
    console.log('\n========== 增强跨平台支持功能测试完成 ==========');
}

/**
 * Windows 平台特定测试
 */
async function testWindowsSpecific(): Promise<void> {
    console.log('\n--- Windows 平台特定测试 ---');
    
    // 测试 Windows 路径处理
    console.log('测试 Windows 路径转换:');
    const winPaths = [
        'C:\\Users\\admin\\nginx\\conf\\nginx.conf',
        'D:\\Projects\\webapp\\dist',
        'C:\\Program Files\\nginx'
    ];
    
    const platformHelper = PlatformHelper.getInstance();
    
    for (const winPath of winPaths) {
        const containerPath = await platformHelper.convertPathForContainer(winPath);
        console.log(`Windows路径: ${winPath}`);
        console.log(`容器路径: ${containerPath}`);
        console.log('---');
    }
    
    // 测试 Windows 下的命令路径查找
    try {
        console.log('测试 Windows 下命令路径查找:');
        const envDetector = new EnvironmentDetector();
        const env = await envDetector.detect();
        console.log(`Docker 路径: ${env.type === 'docker' ? env.path : '未找到'}`);
    } catch (error) {
        console.error('Windows 命令查找失败:', error);
    }
}

/**
 * macOS 平台特定测试
 */
async function testMacOSSpecific(): Promise<void> {
    console.log('\n--- macOS 平台特定测试 ---');
    
    const platformHelper = PlatformHelper.getInstance();
    await platformHelper.checkUserPrivileges();
    
    // 测试 macOS 端口权限处理
    console.log('测试 macOS 端口权限处理:');
    
    const ports = [80, 443, 3000, 8080];
    for (const port of ports) {
        const isPrivileged = platformHelper.isPrivilegedPort(port);
        console.log(`端口 ${port} 需要管理员权限: ${isPrivileged ? '是' : '否'}`);
        
        if (isPrivileged) {
            console.log(`  建议使用: ${platformHelper.suggestNonPrivilegedPort(port)}`);
            console.log(`  可以使用以下方法解决:`);
            console.log(`  1. 使用非特权端口 (>1024)`);
            console.log(`  2. 使用 authbind 或 pfctl 转发端口`);
            console.log(`  3. 使用 nginx 配置反向代理到非特权端口`);
        }
    }
    
    // 测试 macOS 路径处理
    console.log('\n测试 macOS 路径处理:');
    const macPaths = [
        '/Users/admin/Projects/nginx/conf/nginx.conf',
        '/Applications/MAMP/conf/nginx/nginx.conf',
        '/opt/homebrew/etc/nginx'
    ];
    
    for (const macPath of macPaths) {
        const containerPath = await platformHelper.convertPathForContainer(macPath);
        console.log(`macOS路径: ${macPath}`);
        console.log(`容器路径: ${containerPath}`);
    }
}

/**
 * WSL 平台特定测试
 */
async function testWSLSpecific(): Promise<void> {
    console.log('\n--- WSL 环境特定测试 ---');
    
    // 测试 WSL 路径转换
    console.log('测试 WSL 路径转换:');
    
    const wslPaths = [
        '/home/user/nginx/nginx.conf',
        '/mnt/c/Users/admin/Documents/nginx',
        '/etc/nginx'
    ];
    
    const platformHelper = PlatformHelper.getInstance();
    
    for (const wslPath of wslPaths) {
        try {
            const containerPath = await platformHelper.convertPathForContainer(wslPath);
            console.log(`WSL路径: ${wslPath}`);
            console.log(`容器路径: ${containerPath}`);
            console.log('---');
        } catch (error) {
            console.error(`WSL 路径转换失败: ${wslPath}`, error);
        }
    }
    
    // 测试 WSL 中的 Docker 选项
    console.log('测试 WSL 中的 Docker 选项:');
    const dockerOptions = platformHelper.getContainerOptions('docker');
    console.log('Docker 选项:', dockerOptions);
    
    // 测试 WSL 中的环境检测
    console.log('测试 WSL 中的环境检测:');
    const envDetector = new EnvironmentDetector();
    const env = await envDetector.detect();
    console.log('环境类型:', env.type);
    console.log('WSL 特定环境信息:', env.platform);
}

// 执行测试
testCrossPlatformSupport().catch(error => {
    console.error('跨平台支持测试失败:', error);
}); 